import { useEffect, useState } from "react";
import { Avatar, message, Spin } from "antd";
import { userApi } from "../../api/user";
import { User } from "../../api/types/user";

const UserProfile = ({ userId = 2 }: { userId: number }) => {
  const [user, setUser] = useState<User>();
  const [loading, setLoading] = useState(true);

  useEffect(() => {
    const fetchData = async () => {
      try {
        const response = await userApi.createNotice(userId, {
          showError: true, // 默认开启错误提示
        });
        setUser(response.data);
      } catch (err) {
        message.error("加载用户信息失败");
      } finally {
        setLoading(false);
      }
    };

    fetchData();
  }, [userId]);

  if (loading) return <Spin />;

  return (
    <div>
      <Avatar src={user?.avatar} />
      <h2>{user?.name}</h2>
      <p>{user?.email}</p>
    </div>
  );
};

export default UserProfile;
